package com.google.project.client.HumanResources;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.project.client.Service.SpecializeService;
import com.google.project.client.Service.SpecializeServiceAsync;
import com.google.project.shared.SpecializeDTO;

public class HMSkillList extends Composite {

	private static final Binder binder = GWT.create(Binder.class);
	@UiField Button btnAdd;
	@UiField(provided = true) CellTable<SpecializeDTO> ctSpecList = new CellTable<SpecializeDTO>();
	@UiField Button btnDelete;
	@UiField SimplePager pager;
	final MultiSelectionModel<SpecializeDTO> selectionModel = new MultiSelectionModel<SpecializeDTO>();
	private final SpecializeServiceAsync specService = GWT.create(SpecializeService.class);

	interface Binder extends UiBinder<Widget, HMSkillList> {
	}
	
	public HMSkillList() {
		initWidget(binder.createAndBindUi(this));
		bindData();
	}

	/*
	 * Fill data to ctSpecList
	 */
	public void bindData() {

		specService.getSpecList(new AsyncCallback<List<SpecializeDTO>>() {

			@Override
			public void onSuccess(List<SpecializeDTO> result) {
				if(result.size() == 0) {
					Window.alert("Không có dữ liệu!");
				}
				loadData(result);
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Có lỗi trong quá trình tải thông tin");
			}
		});
	}

	/***
	 * 
	 * @param projectList
	 */
	public void loadData(List<SpecializeDTO> specializeList) {
		// Create a data provider.
		ListDataProvider<SpecializeDTO> dataProvider = new ListDataProvider<SpecializeDTO>();
		dataProvider.refresh();
		// Add the data to the data provider, which automatically pushes it to
		// the widget
		List<SpecializeDTO> list = dataProvider.getList();
		for (int i = 0; i < specializeList.size(); i++) {
			specializeList.get(i).setIndex(i+1);
			list.add(specializeList.get(i));
		}
		initTable();
		// SimplePager
		pager.setPageSize(6);
		pager.setDisplay(ctSpecList);
		
		// Connect the table to the data provider.
		dataProvider.addDataDisplay(ctSpecList);
	}

	//Ham tao Cell table
	public void initTable() {
		//So thu tu
		TextColumn<SpecializeDTO> indexColumn = new TextColumn<SpecializeDTO>() {
			@Override
			public String getValue(SpecializeDTO spec) {
				return spec.getIndex()+"";
			}
		};
		
		//Check box
		ctSpecList.setSelectionModel(selectionModel, DefaultSelectionEventManager.<SpecializeDTO> createCheckboxManager());
		Column<SpecializeDTO, Boolean> checkColumn = new Column<SpecializeDTO, Boolean>(new CheckboxCell(true, false)) {
			@Override
			public Boolean getValue(SpecializeDTO object) {
				// Get the value from the selection model.
				return selectionModel.isSelected(object);
			}
		};
		
		//Ten chuyen mon
		Column<SpecializeDTO, String> specName = new Column<SpecializeDTO, String>(new ClickableTextCell()) {
			@Override
			public String getValue(SpecializeDTO object) {
				return object.getSpecializeName();
			}
		};
		specName.setFieldUpdater(new FieldUpdater<SpecializeDTO, String>() {
			@Override
			public void update(int index, SpecializeDTO spec, String value) {
				HMEntrypoint.get().HMAddSkill(spec.getId());
			}

		});
		
		//Mo ta chuyen mon
		TextColumn<SpecializeDTO> specDescription = new TextColumn<SpecializeDTO>() {
			@Override
			public String getValue(SpecializeDTO spec) {
				return spec.getDescription();
			}
		};

		ctSpecList.setWidth("70%", true);
		ctSpecList.setStyleName("ttable");
		
		ctSpecList.addColumn(indexColumn, "STT");
		ctSpecList.setColumnWidth(indexColumn, 40, Unit.PX);
		
		ctSpecList.addColumn(checkColumn, SafeHtmlUtils.fromSafeConstant("<br/>"));
		ctSpecList.setColumnWidth(checkColumn, 40, Unit.PX);

		ctSpecList.addColumn(specName, "Chuyên môn");
		ctSpecList.setColumnWidth(specName, 30.0, Unit.PCT);
		specName.setCellStyleNames("celltable");
		
		ctSpecList.addColumn(specDescription, "Mô tả");
		ctSpecList.setColumnWidth(specDescription, 50.0, Unit.PCT);
		
	}
	
	@UiHandler("btnAdd")
	void onBtnAddClick(ClickEvent event) {
		HMEntrypoint.get().HMAddSkill();
	}
	
	@UiHandler("btnDelete")
	void onBtnDeleteClick(ClickEvent event) {
		Set<SpecializeDTO> listChecked = selectionModel.getSelectedSet();
		if(listChecked.size() == 0) {
			Window.alert("Bạn chưa chọn để xóa!");
		} else {
			List<SpecializeDTO> list = new ArrayList<SpecializeDTO>();
			for (SpecializeDTO specializeDTO : listChecked) {
				list.add(specializeDTO);
			}
			if (Window.confirm("Bạn có chắc chắn muốn xóa?")) {
				specService.deleteSpecializeList(list, new AsyncCallback<Void>() {
	
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Có lỗi trong quá trình xóa!");
					}
	
					@Override
					public void onSuccess(Void result) {
						HMEntrypoint.get().clearMainPanel();
						HMEntrypoint.get().HMSkillList();
					}
				});
			}
		}
	}
}
